<?php
namespace Admin\Controller;
use Admin\Controller\CommonController;
class DingdanController extends CommonController {
 
    	
  //===================================产品订单

    public function index() {


               $wherearr='';
            if(!empty($_POST['code'])){
              $wherearr['code']=array('eq',trim($_POST['code']));
            }
    
            if(!empty($_POST['stime'])&&empty($_POST['etime'])){
              $wherearr['time']=array('EGT',strtotime($_POST['stime'].' 00:00:00'));
            }
            if(!empty($_POST['etime'])&&empty($_POST['stime'])){
                   $wherearr['time']=array('ELT',strtotime($_POST['etime'].' 24:59:59'));
                       }
            if (!empty($_POST['stime'])&&!empty($_POST['etime'])) {
              $wherearr['time']=array(array('EGT',strtotime($_POST['stime'].' 00:00:00')),array('ELT',strtotime($_POST['etime'].' 24:59:59')));
            }
        


 $count = M('dingdan')->where($wherearr)->count();// 查询满足要求的总记录数
    $Page = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数(25)
  
    //$Page -> setConfig('theme','   %LINK_PAGE%  %FIRST%  %UP_PAGE%  %DOWN_PAGE%   %END% %HEADER%');
    //$Page->rollPage=4;
    $Page -> setConfig('first','首页');
    $Page -> setConfig('last','末页');
    $Page -> setConfig('prev','上一页');
    $Page -> setConfig('next','下一页');

    $Page -> setConfig('theme',' <div style="margin-top:2px">  %FIRST%  %UP_PAGE%    %LINK_PAGE%  %DOWN_PAGE%   %END%  </div> ');

        $show = $Page->show();// 分页显示输

     $data=M('dingdan')->order('id desc')->where($wherearr)->limit($Page->firstRow.','.$Page->listRows)->select();
     foreach ($data as  &$value) {
               $add=M('address')->where(array('id'=>$value['aid']))->find();
              $value['address']=$add['diqu'].$add['address']."(".$add['name'].")".$add['tel'];

              
     }

      $this->assign("page",$show);
      $this->assign("pageCount",$count);
      $this->assign('dingdan',$data);
    	$this->display();
    }
      
   

   



    public function update(){

   if (IS_POST) {
      $status=I('post.status');
      $id=I('post.id');
       if($status==0) {
         $this->redirect('Dingdan/index');
       }else{
        if(M('dingdan')->where('id='.$id)->setField('sjid',$status)) {
           $this->success('指定设计师成功',U('Dingdan/index'));
       }else{
        $this->error('指定设计师失败',U('Dingdan/index'));
       }
}
   }else{
            $id=I('get.id');
   
      $data1=M('dingdan')->where('id='.$id)->find();

      $good=M('order_goods')->where('order_id='.$id)->select();
      foreach ($good as  &$value) {
        $shangpin=M('shangpin')->field('img,name,chengse,guige')->where('id='.$value['goods_id'])->find();
        $value['img']=$shangpin['img'];
        $value['name']=$shangpin['name'];
        $value['chengse']=$shangpin['chengse'];
         $value['guige']=$shangpin['guige'];
      }
        $this->data=$good;



       $add=M('address')->where(array('id'=>$data1['aid']))->find();
                  $data1['address']=$add['diqu'].$add['address'];
                  $data1['name']=$add['name'];
                  $data1['tel']=$add['tel'];

          $this->assign('data1',$data1);
           
            $this->display(); 

            }
        }



    //批量删除信息
    public function del(){

        $ids = $_POST['id'];
      

        $str="('".implode("','",$ids)."')";
      

        if (M("dingdan")->where("id in ".$str)->delete()) {
             M('order_goods')->where("order_id in ".$str)->delete();
            //$this->success('删除成功');
              $this->redirect('Dingdan/index');
        } else {
            $this->success('删除失败');
        }
    }
   
    public function exportExcel($expTitle,$expCellName,$expTableData){
        $xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称
        //$fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定
        $fileName = $expTitle.date('_Ymd');//or $xlsTitle 文件名称可根据自己情况设定
        $cellNum = count($expCellName);  //列
        $dataNum = count($expTableData); //行
        //file_put_contents("log.txt",  var_export($xlsTitle, true), FILE_APPEND);
        //file_put_contents("log.txt",  var_export($dataNum, true), FILE_APPEND);
        vendor("PHPExcel.PHPExcel");
        $objPHPExcel = new \PHPExcel();
        $cellName = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');
 $objPHPExcel->getActiveSheet(0)->setCellValue('A1','订单数据列表');// 设置内容
        $objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格
        $objPHPExcel->getActiveSheet(0)->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER); //居中

        // $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.'  Export time:'.date('Y-m-d H:i:s'));
        for($i=0;$i<$cellNum;$i++){
            $objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);
        }
        // Miscellaneous glyphs, UTF-8
        for($i=0;$i<$dataNum;$i++){
            for($j=0;$j<$cellNum;$j++){
                $objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3),' ' . $expTableData[$i][$expCellName[$j][0]]);
            }
        }
        //设置宽度
        //设置列宽度
        //$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);
        //$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setAutoSize(true);
        //$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setAutoSize(true);
 
        //$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setAutoSize(true);
     
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setAutoSize(true);
        //$objPHPExcel->getActiveSheet()->getColumnDimension('S')->setAutoSize(true);
        // $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(32);
        // ob_clean();
        // ob_end_clean();    //清除缓冲区,避免乱码
        header('pragma:public');
        header('Content-type:application/vnd.ms-PHPExcel;charset=utf-8;name="'.$xlsTitle.'.xls"');
        header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印
        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }





   public function expExcel(){
        $xlsName  = "订单数据列表";
        $xlsCell  = array(
            array('id','ID'),
            array('code','订单编号'),
            array('time','下单日期'),
            array('xingming','收货姓名'),
            array('tel','收货电话'),
            array('address','收货地址'),
            array('htid','订单状态'),
            array('biaoti','备注说明'),
            array('goods_info','商品信息')
           
        );

               $wherearr='';
            if(!empty($_POST['code'])){
              $wherearr['code']=array('eq',trim($_POST['code']));
            }
    
            if(!empty($_POST['stime'])&&empty($_POST['etime'])){
              $wherearr['time']=array('EGT',strtotime($_POST['stime'].' 00:00:00'));
            }
            if(!empty($_POST['etime'])&&empty($_POST['stime'])){
                   $wherearr['time']=array('ELT',strtotime($_POST['etime'].' 24:59:59'));
                       }
            if (!empty($_POST['stime'])&&!empty($_POST['etime'])) {
              $wherearr['time']=array(array('EGT',strtotime($_POST['stime'].' 00:00:00')),array('ELT',strtotime($_POST['etime'].' 24:59:59')));
            }
        

        $xlsModel = M('dingdan');
        $xlsData  = $xlsModel->order('id desc')->where($wherearr)->select();
         foreach ($xlsData as &$value) {
              $value['time']=date('Y-m-d H:i:s',$value['time']);
               $value['htid']=$value['htid']?'已发货':'已下单';
        
           $add=M('address')->where('id='.$value['aid'])->find();
              $value['address']=$add['diqu'].$add['address'];
              $value['xingming']=$add['name'];
              $value['tel']=$add['tel'];
                  $shangpin=M('order_goods')->where('order_id='.$value['id'])->select();
               foreach ($shangpin as &$value1){
                $shangpin1=M('shangpin')->field('img,name,chengse,guige')->where('id='.$value1['goods_id'])->find();
                 $value1['img']=$shangpin1['img'];
                   $value1['name']=$shangpin1['name'];
                     $value1['chengse']=$shangpin1['chengse'];
                      $value1['guige']=$shangpin1['guige'];
                       }
                            $value['dingdan']=$shangpin;
                
                      $goods_info = "";
            foreach ($value["dingdan"] as $t => $m) {
                $goods_info .= "商品名称:" . $m["name"] . "  规格:" . $m["guige"] . "  材质:" . $m["chengse"] . "  购买数量:" . $m["gshul"] . "  重量:".$m["gjinz"]."  颜色:".$m["gyanse"]."  ";
            }
            $value["goods_info"] = $goods_info;
                            } 

        $this->exportExcel($xlsName,$xlsCell,$xlsData);

    }









  public function daijiedan(){
            $id=I('post.id');
            $data = array('htid'=>'1');
             $m=M('dingdan')->where('id='.$id)->setField($data);
              echo 1;


 }


}

?>